package andev.axplorer.FileWatcher;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout.LayoutParams;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import andev.aandroidlib.activity.AActivity;
import andev.aandroidlib.util.FileOperator;
import andev.axplorer.R;

public class TextFileWatcher extends AActivity implements View.OnClickListener {
    private static boolean bMax = false;
    private EditText EditText1;
    private ImageButton ib_close, ib_max;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!bMax) {
            setTheme(android.R.style.Theme_Holo_Dialog_NoActionBar);
        } else {
            setTheme(R.style.max_filewatcher_theme);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_filewatcher);

        ib_close = (ImageButton) findViewById(R.id.ib_close);
        ib_close.setOnClickListener(this);
        ib_max = (ImageButton) findViewById(R.id.ib_max);
        ib_max.setOnClickListener(this);
        if (!bMax) {
            getWindow().setLayout(LayoutParams.MATCH_PARENT, getScreenSize().x);
            ib_max.setImageResource(R.mipmap.ic_window_max);
        } else {
            ib_max.setImageResource(R.mipmap.ic_window_normal);
        }


        EditText1 = (EditText) findViewById(R.id.EditText1);
        /*防止外部点击关闭*/
        setFinishOnTouchOutside(false);

        InitData();
    }

    private void InitData() {
        File file = null;
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            file = (File) bundle.getSerializable("file");
        } else {
            Uri uri = intent.getData();
            try {
                file = new File(new URI(uri.toString()));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        EditText1.append(new FileOperator().getFileText(file));
    }

    @Override
    public void onBackPressed() {
        doublePressExit(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_close:
                finish();
                break;
            case R.id.ib_max:
                bMax = !bMax;
                recreate();
                break;
        }
    }
}
